package com.gec.service;

import com.gec.dao.ProcessConfigMapper;
import com.gec.domain.ProcessConfig;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.zip.ZipInputStream;

/**
 * @author Aniki
 * @date  2021-12-13
 */
@Service
public class WorkFlowServiceImpl implements WorkFlowService {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ProcessConfigMapper configMapper;


    @Override
    public String createDeployment(ProcessConfig processConfig, InputStream is) {
        /**创建压缩流*/
        ZipInputStream zipIs = new ZipInputStream(is);
        /**创建流程部署(写入bpmn、pgn到数据库)*/
        Deployment deploy = repositoryService.createDeployment()
        .addZipInputStream(zipIs)
        .name(processConfig.getNote())
        .deploy();

        String deployId = deploy.getId();
        /**这里封装config数据*/
        processConfig.setDeplomentId(deployId);

        //查询流程定义  根据流程部署iD来查  获取唯一结果
        ProcessDefinition procDef = repositoryService
                .createProcessDefinitionQuery()
                .deploymentId(deployId)
                .singleResult();

        processConfig.setProcDefId(procDef.getId());
        processConfig.setProcKey(procDef.getKey());
        processConfig.setVersion(procDef.getVersion());

        return deployId;
    }
}
